﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Drawing;

namespace WindowsFormsApplication1
{
    public class PlayGrid
    {
        ///Variables
        private Square[,] grid;

        internal Square[,] Grid
        {
            get { return grid; }
            set { grid = value; }
        }


        ///Methods
        public PlayGrid() {
            grid = new Square[10,20];

            //Creer et instacier tous les square
            for (int i = 0; i < 10; ++i) {
                for (int j = 0; j < 20; ++j) {
                    grid[i, j] = new Square(i,j,false,Color.Black);
                }
            }
        }

        //test si une ligne est complete
        public int ValidateGrid(int score)
        {
            int counter = 0;
            //on doit parcourir de bas en haut
            for (int i = 19; i >= 0; --i)
            {
                for (int j = 0; j < 10; ++j)
                {
                    if (grid[j, i].Filled)
                    {
                        counter++;
                    }
                    //si une ligne est complete
                    if (counter == 10)
                    {
                        //si une ligne est complete
                        //on efface ses cases
                        deletecase(i);
                        //on redessine tout
                        drawcase(i);
                        //on augmente le score
                        score++;
                    }
                } counter = 0;
            }
            return score;
        }

        //delete la ligne DEVRA augmenter les points
        public void deletecase(int i)
        {
            for (int c = 0; c < 10; ++c)
            {
                grid[c, i].Filled = false;
            }
        }

        //redessine les cases tjs existantes un cran plus bas
        public void drawcase(int i)
        {
            for (int c = i; c >= 0; --c)
            {
                for (int d = 0; d < 10; ++d)
                {
                    if (grid[d, c].Filled == true)
                    {
                        grid[d, c].Filled = false;
                        grid[d, c + 1].Filled = true;
                    }
                }
            }
        }

        //test de fin de partie 
        public bool endgame()
        {
            for (int c = 0; c < 4; ++c)
            {
                if(grid[4, c].Filled == true) return true;
            }
            return false;


        }
    
        public void DrawGrid(Graphics g){

            Pen pen = new Pen(Color.Black, 1);

            //on parcourt la grille
            for (int i = 0; i < 10; ++i)
            {
                for (int j = 0; j < 20; ++j)
                {
                    if (grid[i, j].Filled)
                    {
                        Brush brush = new SolidBrush(grid[i, j].Color);

                        g.FillRectangle(brush, i * 20, j * 20, 20, 20);  // redraws background
                        g.DrawRectangle(pen, i * 20, j * 20, 20, 20);

                        brush.Dispose();
                    }
                }
            }
            
            pen.Dispose();
        }
    }
}
